#bool
Description: Type conversion to bool
.
def bool(x):
'''
Type conversion to bool
:param x: A variable
:return: The value converted to bool
'''
The following values are converted to False
:
None
- Numeric values equal to 0, including but not limited to
int
,float
- Containers with length 0, including but not limited to empty strings, empty tuples, and empty lists
- Objects whose
__bool__
method returnsFalse
- Objects whose
__len__
method returns 0
Example:
print("0 is", bool(0))
print("10 is", bool(10))
print("'hello world' is", bool('hello world'))
print("'' is", bool(''))
print("None is", bool(None))